<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
use DB;
class GoodsController extends Controller
{
	public function getIndex(Request $request){
		// dd($request->all());
		//pic   图片标志  有pic   显示有图评论  无pic  显示所有评论
		$pic = null;
		$pic = $request->input('pic');
		$gid = $request->input('id');
		//评论数据
		$comment = DB::table('shop_comment')
            ->join('shop_users', 'shop_comment.uid', '=', 'shop_users.uid')
            ->join('shop_goods', 'shop_comment.gid', '=', 'shop_goods.gid')
            ->select('shop_comment.*', 'shop_users.*', 'shop_goods.*')
            ->get();
        // dd($comment);
        $comments = null;
        foreach ($comment as $k=> $v){
            if($v->gid==$gid){
                $comments[] = $v;
            }

        }
         //判断如果无评价
        if(empty($comments)){
        		//php获取对象的属性
            	foreach(get_object_vars($comment[0]) as $k=>$v){
            		$comment[0]->$k = null;
            	}
            	$comments[0] = $comment[0];
            	// dd($comments);
        }
        $coment['count'] = 0;
        $coment['stus0'] = 0;
        $coment['stus1'] = 0;
        $coment['stus2'] = 0;
        $coment['zpic'] = 0;
        // dd($comments);
        //获取总评价数
        //判断有评论  才计评价数
        if($comments[0]->zid!=null){
            $coment['count'] = count($comments);
        }
        //正常人数
        $commen = null;
        foreach($comments as $k=>$v){
        	//正常
        	if($v->zstus==0){
        		$coment['stus0']++;
        	}
        	//正常
        	if($v->zstus==1){
        		$coment['stus1']++;
        	}
        	//正常
        	if($v->zstus==2){
        		$coment['stus2']++;
        	}
        	if($v->zpic!=null){
        		$coment['zpic']++;
        	}
        	//判断是否传过来  有图标志
        	if($pic){
	        	if($v->zpic!=null){
	        		$commen[] = $v;
	        	}
	        	//重新获取数据
	        	$comments = $commen;
        	}
        	
        }
        // dd($comments);
		//获取图片地址信息
		$img = DB::table('shop_img')->where('gid',$gid)->get();
		// dd($img);
		//获取商品详情数据
		$description = DB::table('shop_description')->where('gid',$gid)->first();
		// dd($description);
		//获取商品数据
		$goods = DB::table('shop_goods')->where('gid',$gid)->first();
		// dd($goods);
		//处理商品尺码数据
		$gsize = rtrim($goods->gsize,'@@');
		$gsize = explode('@@',$gsize);
		for($i=0;$i<count($gsize);$i++)
		{
			$gsize[$i];
		}
		//分类id
		$tid = $goods->tid;
		$path = DB::table('shop_type')->where('tid',$tid)->value('path');
		$type['tid'] = explode(',',$path);
			foreach($type['tid'] as $k=>$v){
				$type['tname'][$k] = DB::table('shop_type')->where('tid',$v)->value('tname');
			}
		$collect = [];
		if(session('uid')){
			// 查询所有收藏的gid
			$collect = DB::table('shop_collect')->where('uid',session('uid'))->lists('gid');
		}
		
		// dd($type);
		return view('home.goods.goods',['pic'=>$pic,'coment'=>$coment,'comments'=>$comments,'goods'=>$goods,'gsize'=>$gsize,'type'=>$type,'img'=>$img,'description'=>$description,'collect'=>$collect]);
	}
    
}
